import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.io.StringWriter;
import java.nio.CharBuffer;


import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.SVNLogEntryPath;
import org.tmatesoft.svn.core.SVNRevisionProperty;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNDiffClient;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNWCClient;

public class SVNClient {
        SVNUser user_;
        
        public SVNClient() {
                System.out.println("Create Client");
        }
        public static void main (String[] Args) {
                
        }
        public void SetUser(SVNUser user) {
                System.out.println("Set user " + user.name_);   
                user_ = user;
        }
        
        
        Vector<SVNCommit> GetUserCommits() {
                System.out.println("Get " + user_.name_ + " commits");  
                
                DAVRepositoryFactory.setup( );
                Vector<SVNCommit> commits = new Vector<SVNCommit>();    
                try {
                        SVNRepository repository = SVNRepositoryFactory.create( SVNURL.parseURIEncoded( user_.repo_path_ ) );
                        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager( user_.name_, user_.pass_ );
                        repository.setAuthenticationManager( authManager );
                        Collection logEntries  = repository.log( new String[] { "" } , null , 0, repository.getLatestRevision(), true , true );
                        
                        for (Iterator entries = logEntries.iterator(); entries.hasNext(); ) {
                                SVNLogEntry logEntry = (SVNLogEntry) entries.next();
                                if(user_.name_.equals(logEntry.getAuthor())) {
                                        SVNCommit commit = new SVNCommit(logEntry.getRevision(), logEntry.getDate(),  logEntry.getMessage());
                                        commits.add(commit);
                                }
                        }           
        }
        catch ( SVNException svne ) {
                System.out.println(svne);
        }
             
                return commits;
        }
        
        SVNDiff GetDiff(long r1, long r2) {
                System.out.println("Get diff"); 
                
                SVNDiff diff = new SVNDiff();
                
                try {  
                        DAVRepositoryFactory.setup( );
                        
                        SVNRepository repository = SVNRepositoryFactory.create( SVNURL.parseURIEncoded( user_.repo_path_ ) );
                        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager( user_.name_, user_.pass_ );
                        repository.setAuthenticationManager( authManager );
                        
                        SVNDiffClient diffClient = new SVNDiffClient(authManager, null);
                        SVNURL url = SVNURL.parseURIEncoded(user_.repo_path_);
                        SVNRevision rev1 = SVNRevision.create(r1);
                        SVNRevision rev2 = SVNRevision.create(r2);
                        
                        ByteArrayOutputStream os = new ByteArrayOutputStream();
                        
                        diffClient.doDiff(url, rev1, url, rev2, true, false, os);
                        
                    diff.diff_ = os.toString();
                }
                catch (SVNException svne) {
                System.out.println(svne);
            }
                 
                return diff;
        }
}